package com.mobi.Survey.model;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.mobi.Survey.AnswerDecrypter;
import com.mobi.Survey.controller.Pojo;
import com.mobi.Survey.controller.SurveyDetailPojo;
import com.mobi.Survey.uicomponents.ControlFactory;
import com.mobi.Survey.utils.Helper;

/**
 * 
 * <p>
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * SurveyDetailTable.java This table contains detail of Survey. that how many
 * control are there, Each row represents a single control of survey
 * 
 */
public class SurveyDetailTable extends AbstractTable
{
	public static final String TABLE = "survey_detail";

	public static final String COL_ID = "_id";
	public static final String COL_SURVEY_ID = "survey_id";
	public static final String COL_CTRL_TYPE = "ctrl_type";
	public static final String COL_CTRL_TXT = "ctrl_txt";
	public static final String COL_CTRL_PARAM = "ctrl_param";

	public static final String COL_SUBS_ID = "subs_id";
	public static final String COL_CTRL_RESULT_VAL = "ctrl_result_val";
	public static final String COL_MENDATORY_FLAG = "isMendatory";
	public static final String COL_SEQ_NO = "seq_no";
	public static final String COL_DEFAULT_VALUE = "def_Val";
	// CR3 Now Size of Control may also come
	public static final String COL_CTRL_WIDTH = "width";
	public static final String COL_CTRL_HEIGHT = "height";

	public SurveyDetailTable()
	{

	}

	/**
	 * Retrieve data of Controls of survey with answers user submitted
	 * 
	 * @param surveyId
	 * @return
	 */
	public List<SurveyDetailPojo> getSurveyDetailForAnswer(long surveyId)
	{
		List<SurveyDetailPojo> surveyDetails = new ArrayList<SurveyDetailPojo>();
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		// String query = "SELECT * FROM " + TABLE + " where " + COL_SURVEY_ID
		// + "=? ";
		Cursor cursor = db.query(TABLE, null, COL_SURVEY_ID + " = ? and "
				+ COL_CTRL_TYPE + " != ? and " + COL_CTRL_TYPE + " != ? ",
				new String[] { surveyId + "",
						ControlFactory.CONTROL_IMAGE_VIEW + "",
						ControlFactory.CONTROL_VIDEO_VIEW + "" }, null, null,
				null);
		if (cursor != null)
		{

			if (cursor.moveToFirst())
			{
				do
				{

					long id = cursor.getLong(0);
					byte ctr_type = (byte) cursor.getInt(2);
					String ctrText = cursor.getString(3);
					String ctrParameter = cursor.getString(4);
					int subsId = Helper.toInt(cursor.getString(5));
					String acualValue = AnswerDecrypter.decrypt(cursor
							.getString(6));
					boolean isMendatory = cursor.getInt(7) == 1;
					int seqNo = cursor.getInt(8);
					String defVal = cursor.getString(9);

					surveyDetails.add(new SurveyDetailPojo()
							.setControlParameter(ctrParameter)
							.setControlText(ctrText).setControlType(ctr_type)
							.setActualValue(acualValue).setId(id)
							.setMendatory(isMendatory)
							.setSubscriptionId(subsId).setSequanceNo(seqNo)
							.setDefaultValue(defVal));

				} while (cursor.moveToNext());

			}

			cursor.close();
			cursor = null;
		}

		return surveyDetails;
	}

	/**
	 * return data of control of of survey
	 * 
	 * @param surveyId
	 * @return
	 */
	public List<SurveyDetailPojo> getSurveyDetail(long surveyId)
	{
		List<SurveyDetailPojo> surveyDetails = new ArrayList<SurveyDetailPojo>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		// String query = "SELECT * FROM " + TABLE + " where " + COL_SURVEY_ID
		// + "=? ";
		Cursor cursor = db.query(TABLE, null, COL_SURVEY_ID + " = ? ",
				new String[] { surveyId + "" }, null, null, null);

		// Cursor cursor = db.rawQuery(query, new String[] { surveyId+"" });

		if (cursor != null)
		{

			SurveyDetailPojo sdp;
			if (cursor.moveToFirst())
			{
				do
				{

					long id = cursor.getLong(0);

					byte ctr_type = (byte) cursor.getInt(2);
					String ctrText = cursor.getString(3);
					String ctrParameter = cursor.getString(4);
					int subsId = Helper.toInt(cursor.getString(5));
					String actualValue = AnswerDecrypter.decrypt(cursor
							.getString(6));
					boolean isMendatory = cursor.getInt(7) > 0;
					int seqNo = cursor.getInt(8);
					String defVal = cursor.getString(9);
					int width = cursor.getInt(10);
					int height = cursor.getInt(11);
					sdp = new SurveyDetailPojo()
							.setControlParameter(ctrParameter)
							.setControlText(ctrText).setControlType(ctr_type)
							.setActualValue(actualValue).setId(id)
							.setMendatory(isMendatory)
							.setSubscriptionId(subsId).setSequanceNo(seqNo)
							.setDefaultValue(defVal).setSurveyId(surveyId)
							.setPercentWidth(width).setPercentHeight(height);
					surveyDetails.add(sdp);

				} while (cursor.moveToNext());

			}

			cursor.close();
			cursor = null;
		}

		return surveyDetails;
	}

	public SurveyDetailTable(Context ctx)
	{
		super(ctx);
	}

	/**
	 * Insert a record of single control for given survey id
	 */
	public long insertRow(Pojo obj, long surveyId)
	{
		SurveyDetailPojo sdp = (SurveyDetailPojo) obj;

		SQLiteDatabase db = dbHelper.getWritableDatabase();

		ContentValues cv = new ContentValues();

		cv.put(COL_SURVEY_ID, surveyId);
		cv.put(COL_CTRL_TYPE, sdp.getControlType());

		cv.put(COL_SUBS_ID, sdp.getSubscriptionId());

		cv.put(COL_CTRL_TXT, sdp.getControlText());
		cv.put(COL_CTRL_RESULT_VAL, sdp.getActualValue());
		cv.put(COL_CTRL_PARAM, sdp.getControlParameter());
		cv.put(COL_MENDATORY_FLAG, sdp.isMendatory());
		cv.put(COL_SEQ_NO, sdp.getSequanceNo());
		cv.put(COL_DEFAULT_VALUE, sdp.getDefaultValue());
		// CR3 Now Size of Control may also come
		cv.put(COL_CTRL_WIDTH, sdp.getPercentWidth());
		cv.put(COL_CTRL_HEIGHT, sdp.getPercentHeight());
		long id = db.insert(TABLE, null, cv);
		return id;

	}

	public boolean isSubscriptionExist(long subsId)
	{
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.query(TABLE, new String[] { COL_SUBS_ID },
				COL_SUBS_ID + " = ? ", new String[] { subsId + "" }, null,
				null, null);
		boolean isExist = false;

		if (cursor != null)
		{
			if (cursor.moveToFirst() && cursor.getCount() > 0)
			{
				isExist = true;
			}
			cursor.close();
		}

		return isExist;

	}

	@Override
	public long insertRow(Pojo obj)
	{

		return 0;
	}

	@Override
	public boolean createTable(SQLiteDatabase sqliteDb)
	{
		String query = "create table " + TABLE + "( " + COL_ID
				+ " integer primary key autoincrement, " + COL_SURVEY_ID
				+ " integer not null,  " + COL_CTRL_TYPE
				+ " integer not null, " + COL_CTRL_TXT + " text, "
				+ COL_CTRL_PARAM + " text, " + COL_SUBS_ID + " integer, "
				+ COL_CTRL_RESULT_VAL + " text, " + COL_MENDATORY_FLAG
				+ " integer not null, " + COL_SEQ_NO + "  integer, "
				+ COL_DEFAULT_VALUE + " text, " + COL_CTRL_WIDTH + " integer not null, "
				+ COL_CTRL_HEIGHT + " integer not null)";
		sqliteDb.execSQL(query);
		printMessage(query);
		return true;
	}

	@Override
	public boolean deleteRow(long id)
	{
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		int ret = db.delete(TABLE, COL_SURVEY_ID + " = ? ", new String[] { id
				+ "" });
		return ret > 0;
	}

	@Override
	public void deleteAllData()
	{
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.delete(TABLE, null, null);

	}

}
